// pages/ConsumptionCoupon/index.js
const common = require('../../../public.js')
const utils = require('../../../utils/utils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoadingBusy: false,
    cityId: 0,
    regionId: '',
    provinceId: '',
    discountCouponList: [],
    adminDiscountCouponList: [],
    isCityPage: true,
    from: null,
    imageId: 0,
    active: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onChange(event) {
    // wx.showToast({
    //   title: `切换到标签 ${event.detail.name}`,
    //   icon: 'none',
    // });
  },
  onLoad: function (options) {
    // 处理实名认证返回没有option的问题
    if (options) {
      const provinceId = options.provinceId
      if (!common.isEmptyString(provinceId)) {
        this.data.provinceId = provinceId
        this.setData({
          imageId: provinceIdf
        })
      } else {
        let cityId = options.cityId
        // let cityId = 1858
        this.data.cityId = cityId
        const regionId = options && options.regionId ? options.regionId : ''
       
        this.data.from = options.from
        

        if (regionId != '' && utils.containRegion(regionId)) {
          
          this.data.regionId = regionId
          this.data.isCityPage = false
         
        }
        this.setData({
          imageId: this.data.isCityPage ? cityId : regionId
        })

      }

      const cityName = this.getTopicTitle()
      if (cityName) {
        wx.setNavigationBarTitle({
          title: '第六届' + cityName + '惠民消费季',
        })
      }
    }
  },

  onShow: function () {
    this.getCityCoupons()
  },

  getCouponFinish() {
    this.getCityCoupons()
  },

  /**
   * 获取优惠券列表
   */
  getCityCoupons() {
    this.setData({
      isLoadingBusy: true
    })
    let url = null
    let params = null
    let isProvince = false
    if (!common.isEmptyString(this.data.provinceId)) {
      // 省券
      isProvince = true
      url = '/cityDisCoupon/getCityDisCouponListByProvinceId'
      params = {
        provinceId: this.data.provinceId
      }
    } else {
    
      // 这里区分了获取的是市券还是区券
      if (this.data.isCityPage) {
        // 市券
        url = '/cityDisCoupon/getCityDisCouponListByCityId'
        params = {
          cityId: this.data.cityId
        }
      } else {
        // 区券
        url = '/cityDisCoupon/getCityDisCouponListByRegionId'
        params = {
          regionId: this.data.regionId
        }
      }
    }

    const that = this
    common.post(url, params, function (res) {
      if (res.status === 200) {
        if (isProvince) {
          that.setData({
            discountCouponList: res.data
          })
        } else {
          that.setData({
            discountCouponList: res.data.discountCouponList,
            adminDiscountCouponList: res.data.adminDiscountCouponList
          })
        }
      } else {
        that.setData({
          discountCouponList: [],
          adminDiscountCouponList: []
        })
        common.showRequestErrMsg(res)
      }
    },
      function () {
        that.setData({
          isLoadingBusy: false
        })
      })
  },

  toDetail(e) { // 跳转详情页
    let dcId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/cultureCoupons/cultureCouponsDetail/index?dcId=' + dcId
    })
  },

  toPlatformCoupons(e) {
    let dcId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/cultureCoupons/platformCoupons/index?dcId=' + dcId
    })
  },

  getTopicTitle() {
    return utils.getTopicTitle(!common.isEmptyString(this.data.provinceId) ? this.data.provinceId : this.data.cityId, this.data.regionId)
  },

  getJumpParma() {
    let tmpList = []
    !common.isEmptyString(this.data.provinceId) && tmpList.push('provinceId=' + this.data.provinceId)
    !common.isEmptyString(this.data.cityId) && tmpList.push('cityId=' + this.data.cityId)
    !common.isEmptyString(this.data.regionId) && tmpList.push('regionId=' + this.data.regionId)
    return tmpList.join('&')
  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    return {
      title: '第六届' + this.getTopicTitle() + '惠民消费季',
      imageUrl: 'https://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/xfj/share.jpg',
      success: (res) => { },
      fail: (res) => {
        // 分享失败
      }
    }
  },
  onShareTimeline() {
    const query = 'from=timeline&' + this.getJumpParma()
    return {
      title: '第六届' + this.getTopicTitle() + '惠民消费季',
      query
    }
  },
})